package org.mipt.infsec.steganography.api.datastorage.image;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.String;

/**
 * Class describing image
 */
public class Image
{
    /**
     * Content of the image
     * @see BufferedImage
     */
    private BufferedImage image;
    /**
     * ContentType - extension
     */
    private static String extension = "PNG";

    public Image(InputStream is) throws IOException
    {
        image = ImageIO.read(is);

    }

    public Image(BufferedImage image)
    {
        this.image = image;
    }

    public BufferedImage getContent()
    {
        return this.image;
    }

    /**
     * Get extension of image
     * @return extension
     */
    public String getExtension()
    {
        return extension;
    }

    /**
     * Set extension of image standart
     * @param extension string contain MEMI-name
     */
    public void setExtension(String extension)
    {
        this.extension = extension;
    }


    /**
     * Get pixel color from coordinates
     * @param x abscissa
     * @param y ordinate
     * @return array of color (R, G, B, palette)
     */
    public int[] getPixel(int x, int y)
    {
        int[] color = new int[4];

        int colorFF = image.getRGB(x,y);
        //Red color
        color[0] = (colorFF & 0x00ff0000) >> 16;
        //Green color
        color[1] = (colorFF & 0x0000ff00) >> 8;
        //Blue color
        color[2] = (colorFF & 0x000000ff);
        //palitra info
        color[3] = (colorFF & 0xff000000) >> 24;/*
        System.out.println("old " + color[0] + "-" + color[1] + "-" + color[2]);*/
        return color;
    }

    /**
     * Set pixel color to point
     * @param x abscissa
     * @param y ordinate
     * @param RGB color array
     */
    public void setPixel(int x, int y, int[] RGB)
    {
        int colorFF = (RGB[3] << 24) |(RGB[0] << 16) | (RGB[1] << 8) | RGB[2];/*
        System.out.println("new " + RGB[0] + "-" + RGB[1] + "-" + RGB[2]);*/
        image.setRGB(x, y, colorFF);
    }

    /**
     * Get width of the image
     * @return width
     */
    public Integer getWidth()
    {
        return image.getWidth();
    }

    /**
     * get height of the image
     * @return height
     */
    public Integer getHeight()
    {
        return image.getHeight();
    }

    /**
     * Write picture in file
     * @param outFile file which will be contain image
     * @throws IOException occurs when there's any IO error's
     */
    public void write(File outFile) throws IOException
    {
        ImageIO.write(getContent(), getExtension(), outFile);
    }
}
